!function() {
    var userTable = document.getElementById('userTable');
    var newUserBTN=$('newUserBtn');
    var dialog = $('dialog');
    var saveBtn = $('saveBtn');
    var deleBtn = $('deleBtn');
    


    newUserBtn.onclick = function() {
        dialog.getElementsByTagName('form')[0].reset();
        dialog.style.display = 'block';
    };
     deleBtn.onclick = function() {
        alert(999);
    };
    dialog.getElementsByTagName('i')[0].onclick = function() {
        dialog.style.display = 'none';
    };
   saveBtn.onclick = function() {
        var username = $('username').value;
        var name = $('name').value;
        var url = '../api.php/employ';
        var params = 'username='+username+'&name='+name+'&password=abc&gender=333&edu=444&join_date=2017-04-05';
        ajax('post',url,params,function(response) {
            dialog.style.display = 'none';
            getData();
        });
    };
    var getData = function() {
        var url = '../api.php/employ?transform=1&order=id,desc';
        ajax('get',url,null,function(response) {
            render(response.employ);
        });
    };
    var render = function(list) {
        var trArr = [];
        //var list = response;
        
        var len = list.length;
        var clsName;
        for (var i=0;i<len;i++) {
            // if (i % 2 ==1) {
            //     clsName = "pure-table-odd";
            // }else {
            //     clsName = "";
            // }
            clsName = (i % 2 ==1) ? 'pure-table-odd' : '';
            trArr.push(
                '<tr class="',clsName,'">',
                '<td>',i+1,'</td>',
                '<td><input type="checkbox"></td>',
                '<td>', list[i].username, '</td>',
                '<td>', list[i].name, '</td>',
                '<td>', list[i].gender, '</td>',
                '<td>', list[i].edu, '</td>',
                '<td>', list[i].join_date, '</td>',
            '</tr>'
            );
        }
        userTable.getElementsByTagName('tbody')[0].innerHTML = trArr.join('');
    
    };
    window.onload = getData;
}();